package client.comm.baoding.ui

import android.view.View
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.adapter.TxjlAdapter
import client.comm.baoding.api.bean.TxJl
import client.comm.baoding.databinding.ActivityTxjlBinding
import client.comm.baoding.ui.vm.TxjlViewModel
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.adapter.TaxFeesjlAdapter
import client.comm.baoding.databinding.ActivityTaxjlBinding
import client.comm.baoding.ui.vm.TiShuijlViewModel

/**
 * 提税记录
 */
class TiShuijlActivity : BaseMiddleActivity<TiShuijlViewModel, ActivityTaxjlBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_taxjl
    }

    val adapter: TaxFeesjlAdapter by lazy {
        TaxFeesjlAdapter(this, mImgLoader)
    }

    override fun initView() {
        binding?.apply {
            event = this@TiShuijlActivity
//            vm = viewModel
            recyclerView.adapter = adapter
            recyclerView.addItemDecoration(SpaceItemDecoration(10))
            emptyLayout.setOnClickListener { viewModel.holdOne() }
            refreshLayout.setOnRefreshListener {
                adapter.hasMoreState.value = true
                viewModel.pageOne.value = 0
                viewModel.holdOne()
            }
            recyclerView.onLoadMore = {
                if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                    viewModel.holdOne()
                }
            }
        }
    }

    override fun observerUI() {
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.withdraw_list as MutableList<TxJl.Withdraw?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        viewModel.type.observe(this, {
            /*if (it.equals("2"))
                binding.title.text = "提股权记录"*/

        })
    }

    override fun initData() {
        viewModel.type.value = "${intent.getIntExtra("type", 2)}"
        viewModel.holdOne()
    }

    override fun initViewModel(): TiShuijlViewModel = createViewModel()

}